package com.lehecai.core.search.api;

import com.lehecai.core.bean.PairValue;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class SearchApiRequestSet {

    /**
     * 需要设置的字段
     */
    private List<PairValue<String, Object>> fieldAndValueList = new ArrayList<PairValue<String, Object>>();

    public JSON toJSON() {
        JSONObject jsonObject = new JSONObject();

        JSONArray fieldArray = new JSONArray();
        JSONArray valueArray = new JSONArray();

        for (PairValue<String,Object> fieldAndValue : this.fieldAndValueList) {
            fieldArray.add(fieldAndValue.getLeft());
            valueArray.add(fieldAndValue.getRight());
        }

        jsonObject.put(SearchApiConstant.API_PARAMETER_SET_FIELD, fieldArray);
        jsonObject.put(SearchApiConstant.API_PARAMETER_SET_VALUE, valueArray);
        
        return jsonObject;
    }

    public void setUpdate(String field, Object value) {
        this.fieldAndValueList.add(new PairValue<String, Object>(field, value));
    }

}
